<template>
	<el-drawer :title="titleMap[mode]" v-model="visible" :size="1680" destroy-on-close @closed="$emit('closed')">
		<el-container v-loading="loading">
			<el-main style="padding:0 20px 20px 20px">
				<el-form ref="ruleForm" :model="form" :rules="rules" label-width="100px">

					<el-collapse v-model="activeNames">
						<el-collapse-item title="基础设置" name="基础设置">

							<template #title>
								<div class="class_title">1.基础设置</div>
							</template>

							<el-form-item style="margin-top: 18px;" label="分群类目" prop="category_id">
								<blCategory style="width: 400px;" v-model="form.category_id" model="CustGroup" />
							</el-form-item>
							<el-form-item label="分群名称" prop="cg_name">
								<el-input v-model="form.cg_name" style="width: 400px;"></el-input>
							</el-form-item>
							<el-form-item label="分群描述" prop="cg_description">
								<el-input type="textarea" v-model="form.cg_description" :autosize="{minRows: 4}"
									placeholder="请输入内容" show-word-limit maxlength="1000"></el-input>
							</el-form-item>
							<el-form-item label="分群类型" prop="cg_type">
								<el-radio-group style="width: 400px;" v-model="form.cg_type">
									<el-radio-button label="SQL">SQL</el-radio-button>
									<el-radio-button label="规则">规则</el-radio-button>
									<el-radio-button label="静态">静态</el-radio-button>
								</el-radio-group>
							</el-form-item>
							<el-form-item label="是否启用" prop="cg_status">
								<el-radio-group v-model="form.is_disabled">
									<el-radio :label="false">是</el-radio>
									<el-radio :label="true">否</el-radio>
								</el-radio-group>
							</el-form-item>
							<el-form-item v-if="form.cg_type=='SQL'" prop="cg_code">
								<scCodeEditor v-model="form.cg_sql" style="width: 100%;"></scCodeEditor>
								<el-button type="primary" style="margin-top: 10px;"
									@click="test(form.cg_sql)">测试</el-button>
							</el-form-item>

							<div v-if="form.cg_type=='规则'">

								<el-form-item label="宏脉标签" prop="cg_hmsoft_tag">
									<taCascader :params="{
										 key: '客户标签',
										 type: 'tree'
									}" :apiObj="$API.system.custGroup.dictionary" v-model="form.cg_hmsoft_tag" style="width: 300px;"></taCascader>
								</el-form-item>

								<div style="padding-left: 16px;">
									<div class="section-title">进入规则</div>
								</div>
								<div style="padding-left: 32px;">
									<andOr :debug="false" v-model="form.cg_rules.include"></andOr>
								</div>

								<div style="padding-left: 16px;">
									<div class="section-title">退出规则</div>
								</div>
								<div style="padding-left: 32px;">
									<andOr :debug="false" v-model="form.cg_rules.exclude"></andOr>
								</div>

							</div>


						</el-collapse-item>

						<el-collapse-item title="高级设置" name="高级设置">
							<template #title>
								<div class="class_title">2.高级设置</div>
							</template>

							<el-form-item style="margin-top: 18px;" label="计划任务" prop="cg_cron">
								<sc-cron style="width:400px;" v-model="form.cg_cron" placeholder="请输入Cron定时规则" clearable
									:shortcuts="shortcuts">
								</sc-cron>
							</el-form-item>

							<el-form-item label="更新方式" prop="cg_update_type">
								<el-radio-group style="width: 400px;" v-model="form.cg_update_type">
									<el-radio-button label="全量更新">全量更新</el-radio-button>
									<el-radio-button label="增量更新">增量更新</el-radio-button>
									<el-radio-button label="手动更新">手动更新</el-radio-button>
								</el-radio-group>
							</el-form-item>

							<el-form-item label="前置任务" prop="cg_pre_oper" v-if="false">
								<sc-form-table ref="table" v-model="form.cg_pre_oper" :addTemplate="addTemplate"
									drag-sort placeholder="暂无数据">
									<el-table-column prop="tag_name" label="对应标签" width="200" align="center">
										<template #default="scope">
											<el-input v-model="scope.row.tag_name"></el-input>
										</template>
									</el-table-column>
									<el-table-column prop="pre_oper" label="前置操作" width="200" align="center">
										<template #default="scope">
											<el-select v-model="scope.row.pre_oper" placeholder="请选择">
												<el-option label="清空数据" value="清空数据"></el-option>
											</el-select>
										</template>
									</el-table-column>
								</sc-form-table>
							</el-form-item>
						</el-collapse-item>
					</el-collapse>

					<div style="height:400px">
						&nbsp;
					</div>

				</el-form>
			</el-main>
			<el-footer>
				<el-button type="primary" :loading="isSaveing" @click="submit">保存</el-button>
				<el-button @click="visible=false">取消</el-button>
			</el-footer>
		</el-container>
	</el-drawer>
</template>

<script>
	var _this;

	import {
		defineAsyncComponent
	} from 'vue';
	const scEditor = defineAsyncComponent(() => import('@/components/scEditor'));

	import andOr from './andOr/index.vue'

	export default {
		emits: ['success', 'closed'],
		components: {
			scEditor,
			andOr
		},
		data() {
			return {
				andOrObj: {},
				addTemplate: {
					tag_name: '客户标签',
					pre_oper: ''
				},
				shortcuts: [{
					text: "每天0点5分",
					value: "0 5 0 * * ?"
				}],
				activeNames: ['基础设置', '高级设置'],
				props: {
					label: 'title',
					value: 'id',
					keyword: 'keyword'
				},
				activeTab: 'basic',
				data: {

				},
				loading: false,
				mode: "add",
				titleMap: {
					add: '添加分群',
					edit: '编辑分群'
				},
				form: {
					id: '',
					cg_name: "",
					category_id: null,
					cg_description: "",
					cg_type: "规则",
					cg_status: "待更新",
					is_disabled: true,
					cg_rules: {
						include: {},
						exclude: {}
					},
					cg_hmsoft_tag: [],
					cg_sql: "",
					cg_cron: "0 5 0 * * ?",
					cg_update_type: '增量更新',
					cg_pre_oper: []
				},
				rules: {
					cg_name: [{
						required: true,
						message: '请填写名称'
					}]
				},
				visible: false,
				isSaveing: false,
				apiObj: null,
			}
		},
		mounted() {
			_this = this;
		},
		methods: {
			test(params) {

				if (typeof params == 'string') {

					this.loading = true
					this.$API.system.custGroup.test.post({
						cg_sql: params
					}).then(res => {
						this.$alert(res.msg, res.code == 0 ? '提示' : '错误', {
							type: res.code == 0 ? 'success' : 'error'
						})
					}).finally(() => {
						this.loading = false
					})

				}
			},
			getNextCronTime(str) {
				// return getNextCronTime(str);
			},
			success(res) {
				return true;
			},
			//显示
			open(data) {
				if (data) {
					this.mode = data;
				}
				this.visible = true;
				this.$nextTick(() => {
					if (this.mode == 'add') {
						//this.$refs.table.rowAdd()
					}
				})
				return this;
			},
			//表单提交方法
			submit() {
				this.$refs.ruleForm.validate(async (data) => {
					if (data) {
						var res;
						this.isSaveing = true;
						if (this.mode == 'add') {
							res = await _this.$API.system.custGroup.create.post(this.form);
						} else {
							res = await _this.$API.system.custGroup.update.post(this.form);
						}
						this.isSaveing = false;
						if (res.code == 0) {
							this.$emit('success', data, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						} else {
							this.$alert(res.message, "提示", {
								type: 'error'
							})
						}
					}
				})
			},
			//表单注入数据
			setData(data) {
				this.loading = true

				if (!data.id) {
					if (data.category_id) {
						this.form.category_id = data.category_id
					}
					this.loading = false
					return;
				}

				const params = {
					id: data.id
				}
				setTimeout(async () => {
					var res = await this.$API.system.custGroup.show.get(params)
					this.loading = false
					this.form = res.data
					this.form.cg_rules = this.form.cg_rules || {};
					if (!this.form.cg_rules.exclude) {
						this.form.cg_rules.exclude = {};
					}
					if (!this.form.cg_rules.include) {
						this.form.cg_rules.include = {};
					}
				}, 0)
			}
		}
	}
</script>

<style>
	.custom-empty {
		width: 100%;
		height: 100%;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		background: #8c939d;
		border-radius: 5px;
	}

	.custom-empty i {
		font-size: 30px;
		color: #fff;
	}

	.custom-empty p {
		font-size: 12px;
		font-weight: normal;
		color: #fff;
		margin-top: 10px;
	}

	.class_title {
		padding-left: 18px;
		font-size: 18px;
		background-color: #f4f9ff;
		text-align: left;
		width: 100%;
	}

	.section-title {
		font-size: 14px;
		font-weight: 400;
		border-left: 5px solid var(--el-color-primary);
		padding-left: 10px;
		color: var(--el-text-color-regular);
	}
</style>
